/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sputty.classes;

import ch.ethz.ssh2.Connection;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

/**
 *
 * @author mauro
 */
public class MyTerminalArea extends JTextArea {
    private InputStream in;
    private OutputStream out;
    private JPopupMenu mnu;
    private Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    private Connection C;
    
    public MyTerminalArea(){
        init();
    }
    
    private void init(){
        mnu = new JPopupMenu();
        mnu.setBorderPainted(true);
        mnu.setInvoker(this);
        mnu.setFont(new Font(Font.DIALOG, Font.BOLD, 10));
        JMenuItem mnuCopy = new JMenuItem("Copia", new ImageIcon(this.getClass().getResource("/sputty/gui/resources/popupmenus/editcopy.png")));
        mnuCopy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mnuCopyActionListener(e);
            }
        });
        mnuCopy.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent evt){
                mnuHighlightON((JMenuItem)evt.getSource());
            }
            @Override
            public void mouseExited(MouseEvent evt){
                mnuHighlightOFF((JMenuItem)evt.getSource());
            }
        });
        mnuCopy.setFont(new Font(Font.DIALOG, Font.BOLD, 10));
        mnu.add(mnuCopy);
        JMenuItem mnuPaste = new JMenuItem("Incolla", new ImageIcon(this.getClass().getResource("/sputty/gui/resources/popupmenus/editpaste.png")));
        mnuPaste.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mnuPasteActionListener(e);
            }
        });
        mnuPaste.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent evt){
                mnuHighlightON((JMenuItem)evt.getSource());
            }
            @Override
            public void mouseExited(MouseEvent evt){
                mnuHighlightOFF((JMenuItem)evt.getSource());
            }
        });
        mnuPaste.setFont(new Font(Font.DIALOG, Font.BOLD, 10));
        mnu.add(mnuPaste);
        JMenuItem mnuCopyNPaste = new JMenuItem("Copia & Incolla", new ImageIcon(this.getClass().getResource("/sputty/gui/resources/popupmenus/cpynpaste.png")));
        mnuCopyNPaste.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mnuCopyNPasteActionListener(e);
            }
        });
        mnuCopyNPaste.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent evt){
                mnuHighlightON((JMenuItem)evt.getSource());
            }
            @Override
            public void mouseExited(MouseEvent evt){
                mnuHighlightOFF((JMenuItem)evt.getSource());
            }
        });
        mnuCopyNPaste.setFont(new Font(Font.DIALOG, Font.BOLD, 10));
        mnu.add(mnuCopyNPaste);
        
        mnu.addSeparator();
        
        JMenuItem mnuDisconnect = new JMenuItem("Disconnetti", new ImageIcon(this.getClass().getResource("/sputty/gui/resources/popupmenus/logout.png")));
        mnuDisconnect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mnuDisconnectActionListener(e);
            }
        });
        mnuDisconnect.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent evt){
                mnuHighlightON((JMenuItem)evt.getSource());
            }
            @Override
            public void mouseExited(MouseEvent evt){
                mnuHighlightOFF((JMenuItem)evt.getSource());
            }
        });
        mnuDisconnect.setFont(new Font(Font.DIALOG, Font.BOLD, 10));
        mnu.add(mnuDisconnect);
        
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MyTerminalAreaMouseClicked(e);
            }
        });
    }
    
    public void setInputStream(InputStream in){
        this.in = in;
    }
    
    public InputStream getInputStram(){
        return this.in;
    }
    
    public void setOutputStream(OutputStream out){
        this.out = out;
    }
    
    public OutputStream getOutputStream(){
        return this.out;
    }
    
    public void setConnection(Connection C){
        this.C = C;
    }
    
    public Connection getConnection(){
        return this.C;
    }
    
    private void MyTerminalAreaMouseClicked(MouseEvent e){
        if(e.getButton() == e.BUTTON3){
            mnu.setLocation(e.getXOnScreen(), e.getYOnScreen());
            mnu.setVisible(true);
            mnu.grabFocus();
        }
    }
    
    private void mnuCopyActionListener(ActionEvent e){
        if(this.getSelectionStart() > 0){
            StringSelection s = new StringSelection(this.getSelectedText());
            clip.setContents(s, null);
            mnu.setVisible(false);
            this.grabFocus();
        }
    }
    
    private void mnuPasteActionListener(ActionEvent e){
        if(clip.isDataFlavorAvailable(DataFlavor.stringFlavor)){
            try{
                Transferable T = clip.getContents(null);
                String[] data = new String[1];
                data[0] = (String)T.getTransferData(DataFlavor.stringFlavor);
                new SendCommands(in, out, data, 0,false).run();
                mnu.setVisible(false);
                this.grabFocus();
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    
    private void mnuCopyNPasteActionListener(ActionEvent e){
        if(this.getSelectionStart() > 0){
            StringSelection s = new StringSelection(this.getSelectedText());
            clip.setContents(s, null);
            try{
                Transferable T = clip.getContents(null);
                String[] data = new String[1];
                data[0] = (String)T.getTransferData(DataFlavor.stringFlavor);
                new SendCommands(in, out, data, 0,false).run();
                mnu.setVisible(false);
            }catch(Exception ex){
                ex.printStackTrace();
            }
            mnu.setVisible(false);
            this.grabFocus();
        }        
    }
    private void mnuHighlightON(JMenuItem mnuItem){
        mnuItem.setBackground(UIManager.getColor("MenuItem.selectionBackground"));  
        mnuItem.setForeground(UIManager.getColor("MenuItem.selectionForeground"));
    }
    
    private void mnuHighlightOFF(JMenuItem mnuItem){
        mnuItem.setBackground(UIManager.getColor("MenuItem.background"));   
        mnuItem.setForeground(UIManager.getColor("MenuItem.foreground"));
    }
    
    private void mnuDisconnectActionListener(ActionEvent e){
        C.close();        
        ((JTabbedPane)(this.getParent().getParent().getParent().getParent())).remove(((JTabbedPane)(this.getParent().getParent().getParent().getParent())).getSelectedIndex());
        mnu.setVisible(false);
    }
}
